package com.jumi.microservice.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jumi.microservice.common.constant.IsDeletedEnum;
import com.jumi.microservice.common.exception.ExceptionEnum;
import com.jumi.microservice.common.utils.StringProvider;
import com.jumi.microservice.material.dto.JmUnitResponse;
import com.jumi.microservice.material.dto.category.JmCategoryResponse;
import com.jumi.microservice.material.dto.supplier.JmSupplierResponse;
import com.jumi.microservice.material.dto.warehouse.JmWarehouseResponse;
import com.jumi.microservice.material.service.JmWarehouseService;
import com.jumi.microservice.stock.domain.StockMaterialAlarmDTO;
import com.jumi.microservice.stock.domain.StockMaterialDO;
import com.jumi.microservice.stock.dto.StockMaterialRequest;
import com.jumi.microservice.stock.dto.StockMaterialResponse;
import com.jumi.microservice.stock.mapper.StockMaterialMapper;
import com.jumi.microservice.stock.service.CommonService;
import com.jumi.microservice.stock.service.StockMaterialService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName StockMaterialServiceImpl
 * @Description
 * @Author Abel Yang
 * @Date 2020/8/21/021 15:02
 **/
@Service
public class StockMaterialServiceImpl implements StockMaterialService {

    @Resource
    private JmWarehouseService jmWarehouseService;

    @Resource
    private CommonService commonService;

    @Resource
    private StockMaterialMapper stockMaterialMapper;

    @Override
    public List<StockMaterialResponse> getStockMaterialList(StockMaterialRequest jmStockMaterialRequest) {
        List<StockMaterialResponse> stockList = stockMaterialMapper.findAll(jmStockMaterialRequest);
        if (stockList.isEmpty()) {
            return stockList;
        }
        return formatStockMaterialList(stockList);
    }

    @Override
    public int setAlarmNum(StockMaterialAlarmDTO alarmDTO) {
        StockMaterialDO stockMaterial = stockMaterialMapper.selectOne(
                Wrappers.<StockMaterialDO>lambdaQuery()
                        .eq(StockMaterialDO::getMaterialId, alarmDTO.getMaterialId())
                        .eq(StockMaterialDO::getWarehouseId, alarmDTO.getWarehouseId())
                        .eq(StockMaterialDO::getIsDel, IsDeletedEnum.NO)
        );
        ExceptionEnum.FAIL_STOCK_MATERIAL_NOT_EXISTS.doThrowIf(null == stockMaterial);

        assert stockMaterial != null;

        LambdaUpdateWrapper<StockMaterialDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StockMaterialDO::getId, stockMaterial.getId())
                .set(StockMaterialDO::getAlarmNum, alarmDTO.getAlarmNum())
                //是否触发报警
                .set(StockMaterialDO::getAlarmed, alarmDTO.getAlarmNum() >= stockMaterial.getAvailableStockNum());
        return stockMaterialMapper.update(new StockMaterialDO(), updateWrapper);
    }

    private List<StockMaterialResponse> formatStockMaterialList(List<StockMaterialResponse> stockList) {

        //查询商品分类
        List<Long> categoryIds = stockList.stream().map(StockMaterialResponse::getCateId).distinct().collect(Collectors.toList());
        Map<Long, JmCategoryResponse> categoryMap = commonService.getCategoryMapByIds(categoryIds);
        //查询商品单位
        Map<Integer, JmUnitResponse> unitMap = commonService.getUnitMapByIds(stockList.stream().map(StockMaterialResponse::getUnitId).distinct().collect(Collectors.toList()));
        //查询商品供应商
        Map<Long, JmSupplierResponse> supplierMap = commonService.getSupplierMapByIds(stockList.stream().map(StockMaterialResponse::getSupplierId).distinct().collect(Collectors.toList()));
        //查询仓库信息
        Map<Long, String> warehouseMap = jmWarehouseService.getWarehouseList().stream().collect(
                Collectors.toMap(JmWarehouseResponse::getWarehouseId, JmWarehouseResponse::getWarehouseName, (k1, k2) -> k2)
        );
        stockList.forEach(stock -> {
            JmCategoryResponse category = null != categoryMap.get(stock.getCateId()) ? categoryMap.get(stock.getCateId()) : new JmCategoryResponse();
            stock.setCateName(StringProvider.isNotEmpty(category.getCategoryName()) ? category.getCategoryName() : "");

            JmUnitResponse unitInfo = null != unitMap.get(stock.getUnitId()) ? unitMap.get(stock.getUnitId()) : new JmUnitResponse();
            stock.setUnit(StringProvider.isNotEmpty(unitInfo.getSmunitName()) ? unitInfo.getSmunitName() : "");

            JmSupplierResponse supplier = null != supplierMap.get(stock.getSupplierId()) ? supplierMap.get(stock.getSupplierId()) : new JmSupplierResponse();
            stock.setSupplierCompanyName(StringProvider.isNotEmpty(supplier.getSupplierCompanyName()) ? supplier.getSupplierCompanyName() : "");

            stock.setWarehouseName(StringProvider.isNotEmpty(warehouseMap.get(stock.getWarehouseId())) ? warehouseMap.get(stock.getWarehouseId()) : "");
        });

        return stockList;
    }

}
